from application import db
from datetime import datetime

# 粉丝与作者的
authors_followers = db.Table('authors_followers',
                             db.Column('user_id', db.Integer, db.ForeignKey('users.id'), primary_key=True),
                             db.Column('follower_id', db.Integer, db.ForeignKey('followers.id'), primary_key=True)
                             )


# 用户
class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(50), nullable=False)  # 用户昵称
    password = db.Column(db.String(225), nullable=False)
    email = db.Column(db.String(100), nullable=False, unique=True)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now)

    # 头像
    photo = db.Column(db.String(100), nullable=False, default='默认.jpg')
    # 角色
    role = db.Column(db.String(10), nullable=False, default='普通用户')
    # 积分
    credit = db.Column(db.Integer, nullable=False, default=0)
    fans = db.Column(db.Integer, nullable=False, default=0)  # 粉丝
    attention = db.Column(db.Integer, nullable=False, default=0)  # 关注
    support = db.Column(db.Integer, nullable=False, default=0)  # 点赞

    days = db.Column(db.Integer, nullable=False, default=0)  # 用户天数

    # 粉丝关键
    followers = db.relationship('Follower', secondary=authors_followers, backref=db.backref('users', lazy='dynamic'))


# 粉丝表
class Follower(db.Model):
    __tablename__ = 'followers'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100), nullable=False)


# 博客
class QuestionModel(db.Model):
    __tablename__ = "boke_question"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(100), nullable=False)
    content = db.Column(db.Text, nullable=False)
    detail = db.Column(db.Text, nullable=False)
    cover = db.Column(db.Text, nullable=False)  # 封面
    up = db.Column(db.Integer, nullable=False, default=0)  # 点赞
    down = db.Column(db.Integer, nullable=False, default=0)  # 点踩

    credit = db.Column(db.Integer, nullable=False, default=0)  # 文章消耗积分
    readcount = db.Column(db.Integer, nullable=False, default=0)  # 文章阅读次数
    replycount = db.Column(db.Integer, nullable=False, default=0)  # 文章评论数

    recomment = db.Column(db.String(100), nullable=False, default=0)  # 是否为推荐
    draft = db.Column(db.String(100), nullable=False, default=0)  # 是否为草稿
    check = db.Column(db.String(100), nullable=False, default=1)  # 是否被审核
    hidden = db.Column(db.String(100), nullable=False, default=0)  # 隐藏

    create_time = db.Column(db.DateTime, default=datetime.now)  # 创建时间
    update_time = db.Column(db.DateTime, default=datetime.now)  # 修改时间

    # 作者外键
    author_id = db.Column(db.Integer, db.ForeignKey("users.id"))
    # 创立外键
    author = db.relationship('User', backref=db.backref('boke_question'),cascade="all,delete")

    # 与文章id建立外键
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
    # 与分类建立联系
    category = db.relationship('Category', backref=db.backref('boke_question'),cascade="all,delete")

    def __str__(self):
        return self.title


# 点赞表
class Like(db.Model):
    __tablename__ = 'like'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))  # 点赞人
    user = db.relationship('User', backref=db.backref('like'))
    article_id = db.Column(db.Integer, db.ForeignKey('boke_question.id'))  # 点赞的文章
    article = db.relationship('QuestionModel', backref=db.backref('like'),cascade="all,delete")
    # 1为点赞，0为什么都不点，2为点踩
    type = db.Column(db.Integer, nullable=False, default=0)


class AnswerModel(db.Model):
    __tablename__ = "boke_answer"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    content = db.Column(db.Text, nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)

    replyid = db.Column(db.Integer, nullable=False, default=0)  # 子评论

    agreecount = db.Column(db.Integer, nullable=False, default=0)  # 点赞
    opposecount = db.Column(db.Integer, nullable=False, default=0)  # 反对

    # 评论外键
    question_id = db.Column(db.Integer, db.ForeignKey("boke_question.id"))
    question = db.relationship('QuestionModel', backref=db.backref('boke_answer'),cascade="all,delete")

    # 建立与作者的外键
    author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    author = db.relationship('User', backref=db.backref('boke_answer'),cascade="all,delete")

    def __str__(self):
        return self.content


class Credit(db.Model):
    __tablename__ = 'credits'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 建立与作者的外键
    author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    author = db.relationship('User', backref=db.backref('credits'))

    # 类别
    category = db.Column(db.String(50), nullable=False)
    # 操作对象（文章等为id，正常注册为0）
    target = db.Column(db.Integer, nullable=False)
    # 积分增减
    all_credit = db.Column(db.Integer, nullable=False)
    # 新增时间
    create_time = db.Column(db.DateTime, default=datetime.now)
    # 修改时间
    update_time = db.Column(db.DateTime, default=datetime.now)


# 文章收藏
class Collection(db.Model):
    __tablename__ = 'collection'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    canceled = db.Column(db.String(20), nullable=False, default='否')  # 是否被取消收藏
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now)
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now)

    # 建立与作者的外键
    author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    author = db.relationship('User', backref=db.backref('collection'))

    # 文章
    question_id = db.Column(db.Integer, db.ForeignKey("boke_question.id"))
    question = db.relationship('QuestionModel', backref=db.backref('collection'),cascade="all,delete")


class Category(db.Model):
    """分类"""
    __tablename__ = 'category'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(50), nullable=False)
